package com.hellobike.haxing.modules.wms.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.wms.entity.Location;
import com.hellobike.haxing.modules.wms.entity.Partition;
import com.hellobike.haxing.modules.wms.service.ILocationService;
import com.hellobike.haxing.modules.wms.service.IPartitionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: t_wms_location
* @Author: haxing-boot
* @Date:   2020-01-13
* @Version: V1.0
*/
@RestController
@RequestMapping("/wms/location")
@Slf4j
public class LocationController extends WmsBaseController<Location, ILocationService> {
   @Autowired
   private ILocationService locationService;
   @Autowired
   private IPartitionService partitionService;

   /**
    * 分页列表查询
    *
    * @param location
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "t_wms_location-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(Location location,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       location.setWarehouseId(getWarehouseId());
       QueryWrapper<Location> queryWrapper = QueryGenerator.initQueryWrapper(location, req.getParameterMap());
       Page<Location> page = new Page<Location>(pageNo, pageSize);
       page.addOrder(OrderItem.desc("id"));
       IPage<Location> pageList = locationService.page(page, queryWrapper);
       return Result.ok(pageList);
   }

   /**
    *   添加
    *
    * @param location
    * @return
    */
   @AutoLog(value = "t_wms_location-添加",operateType=2)
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody Location location) {
       Long warehouseId = getWarehouseId();
       location.setWarehouseId(warehouseId);
       Long partitionId = location.getPartitionId();
       if(null == partitionId){
           return Result.error("库区数据不存在");
       }
       Partition partition = partitionService.getById(partitionId);
       if(null == partition){
           return Result.error("库区不存在");
       }
       location.setPartitionCode(partition.getPartitionCode());
       Location param = new Location();
       param.setLocCode(location.getLocCode());
       param.setWarehouseId(warehouseId);
       if (locationService.getBaseMapper().selectOne(new QueryWrapper<>(param)) != null) {
           return Result.error("库位编码已被占用");
       }
       if(location.getLocName() == null){
           location.setLocName("");
       }
       locationService.save(location);
       return Result.ok("添加成功！");
   }

    @GetMapping(value = "/getPartition")
    public Result<?> getPartition() {
       Partition param = new Partition();
       param.setWarehouseId(getWarehouseId());
        Wrapper<Partition> wrapper = new QueryWrapper<>(param);
        List<Partition> partitions = partitionService.getBaseMapper().selectList(wrapper);
        return Result.ok(partitions);
    }

   /**
    *  编辑
    *
    * @param location
    * @return
    */
   @AutoLog(value = "t_wms_location-编辑",operateType=3)
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody Location location) {
       Long partitionId = location.getPartitionId();
       if(null == partitionId){
           return Result.error("库区数据不存在");
       }
       Partition partition = partitionService.getById(partitionId);
       if(null == partition){
           return Result.error("库区不存在");
       }
       location.setPartitionCode(partition.getPartitionCode());
       Location location1 = locationService.getById(location.getId());
       if (location1 == null) {
           return Result.error("库位不存在");
       }
       if (!location1.getLocCode().equals(location.getLocCode())) {
           Location param = new Location();
           param.setLocCode(location.getLocCode());
           param.setWarehouseId(getWarehouseId());
           if (locationService.getBaseMapper().selectOne(new QueryWrapper<>(param)) != null) {
               return Result.error("库位编码已被占用");
           }
       }
       if(location.getLocName() == null){
           location.setLocName("");
       }
       locationService.updateById(location);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "t_wms_location-删除",operateType=4)
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       locationService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "t_wms_location-批量删除",operateType=4)
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.locationService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       Location location = locationService.getById(id);
       if(location==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(location);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param location
   */
   @AutoLog(value = "t_wms_location-导出excel",operateType=6)
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, Location location) {
       return super.exportXls(request, location, Location.class, "t_wms_location");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @AutoLog(value = "t_wms_location-导入excel",operateType=5)
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, Location.class);
   }

}
